﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.MODEL.FoundationModel;

namespace MINERP.ComControls
{
   public   class PrinterBill
    {
        public DataSet PrintBill(SHOP_Bill_Info sbinfo)
        {
         
                double Para_TotalNum = 0;//照片总张数
                double Para_TotalAreas = 0;//照片总面积
                double Para_TotalCount = 0;//本期金额
                double Para_Sum = 0;//产品总数
                double Para_TotalPrintCount = 0;//输出金额
                double Para_TotalProductCount = 0;//产品金额

              
              

                bool IsNeedPrintMoney = sbinfo.Shop.IsNeedPrintUPrice;
                DataTable tableTitle = CreateShopBillTitle();
                DataRow titleRow = tableTitle.NewRow();
                titleRow["ShopName"] = sbinfo.Shop.ShopName;
                titleRow["Tel1"] = sbinfo.Shop.Tel1;
                titleRow["Tel2"] = sbinfo.Shop.Tel2;
                titleRow["ShopId"] = sbinfo.Shop.ShopId;
                titleRow["shopKeeper"] = sbinfo.Shop.ShopKeeper;
                titleRow["QQ"] = sbinfo.Shop.QQ;
                titleRow["BillId"] = sbinfo.BillId;
                titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
                titleRow["CreateTime"] = sbinfo.CreateTime;
                titleRow["Adress"] = sbinfo.Shop.Address;
                titleRow["CheckOutType"] = sbinfo.CheckType;
                titleRow["Remark"] = sbinfo.Shop.Remark;
               
                if (IsNeedPrintMoney)
                {
                    titleRow["Balance"] = sbinfo.ShopBalance;
                    titleRow["BillCount"] = sbinfo.BillCount;
                }
                else
                {
                    titleRow["Balance"] = 0;
                    titleRow["BillCount"] = 0;
                }
                titleRow["Salesman"] = sbinfo.Salesman;
                titleRow["CreditLevel"] = sbinfo.CreditLevel == null ? 0 : sbinfo.CreditLevel;
                

                DataTable tableDetail = CreateShopBillDetail();
                DataTable tableSubDetail = CreateShopBillSubDetail();
                List<Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

                foreach (Bill_Detail_Info billDetail in detailList)
                {
                    DataRow row = tableDetail.NewRow();
                    row["BillDetailId"] = billDetail.BillDetailId;
                    row["OrderInfoCode"] = billDetail.OrderInfoCode;
                    row["OrderProductName"] = billDetail.OrderProductName;
                    row["CtmName"] = billDetail.CtmName;
                    row["Remark"] = billDetail.Remark;
                    row["OrderBagName"] = billDetail.OrderBagName;

                    tableDetail.Rows.Add(row);
                    List<Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                    foreach (Bill_Detail_Detail_Info detail in subDetail)
                    {
                        DataRow rowSub = tableSubDetail.NewRow();
                        rowSub["DetailName"] = detail.DetailName;
                        rowSub["BillDetailId"] = billDetail.BillDetailId;
                        rowSub["Spec"] = detail.Spec;
                        rowSub["Num"] = detail.Num;
                        rowSub["UnitPrice"] = detail.UnitPrice;
                        rowSub["DetailRemark"] = detail.DetailRemark;
                        if (IsNeedPrintMoney)
                        {
                            rowSub["Count"] = detail.Count;
                        }
                        else
                        {
                            rowSub["Count"] = 0;
                        }

                        rowSub["Area"] = detail.Area;
                        rowSub["Type"] = detail.Type;
                        tableSubDetail.Rows.Add(rowSub);

                        if (detail.Type == 1)//是输出类型
                        {
                            Para_TotalNum += detail.Num;
                            Para_TotalAreas += detail.Area;
                            Para_TotalPrintCount += detail.Count;
                        }
                        else if (detail.Type == 0)
                        {
                            Para_TotalProductCount += detail.Count;
                        }
                        Para_TotalCount += detail.Count;
                    }
                    Para_Sum++;
                   
                }
               

                titleRow["Para_TotalNum"] = Para_TotalNum;
                titleRow["Para_TotalAreas"] = Para_TotalAreas;
                titleRow["Para_TotalCount"] = Para_TotalCount;
                titleRow["Para_Sum"] = Para_Sum;
                titleRow["Para_TotalPrintCount"] = Para_TotalPrintCount;

                titleRow["Para_TotalProductCount"] = Para_TotalProductCount;
                tableTitle.Rows.Add(titleRow);

                tableTitle.TableName = "tableTitle";

                tableDetail.TableName = "tableDetail";
                tableSubDetail.TableName = "tableSubDetail";



            #region 打印报表
            DataSet set = new DataSet("ds");
            set.Tables.Add(tableTitle);
            set.Tables.Add(tableDetail);
            set.Tables.Add(tableSubDetail);
          

            return set;
            #endregion


        }
        /// <summary>
        /// 产品明细表
        /// </summary>
        /// <returns></returns>
        public DataTable CreatePrintDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("OrderBagId", typeof(int));
            table.Columns.Add("OrderProductId", typeof(int));
            table.Columns.Add("OrderProductCode", typeof(string));
            table.Columns.Add("BagCode", typeof(string));
            table.Columns.Add("BagName", typeof(string));
            table.Columns.Add("ShopName", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("ProductName", typeof(string));
            table.Columns.Add("ProductCode", typeof(string));
            table.Columns.Add("ProductNum", typeof(int));
            table.Columns.Add("CreateTime", typeof(DateTime));
            table.Columns.Add("PreFetchTime", typeof(DateTime));
            table.Columns.Add("OutRemark", typeof(string));
            table.Columns.Add("FetchTime", typeof(DateTime));
            table.Columns.Add("FetchRemark", typeof(string));
            table.Columns.Add("CreateUserName", typeof(string));
            table.Columns.Add("Size", typeof(string));
            table.Columns.Add("PhotoNum", typeof(int));


            return table;
        }
        /// <summary>
        /// 商户信息表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopTop()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ShopId", typeof(int));
            table.Columns.Add("shopName", typeof(string));
            table.Columns.Add("shopKeeper", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("Tel2", typeof(string));
            table.Columns.Add("address", typeof(string));

            return table;

        }
        /// <summary>
        /// 为外商信息表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateOutShopTop()
        {
            DataTable table = new DataTable();
            table.Columns.Add("OutShopId", typeof(int));
            table.Columns.Add("OutShopName", typeof(string));
            table.Columns.Add("Keeper", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("Tel2", typeof(string));
            table.Columns.Add("City", typeof(string));
            table.Columns.Add("Addr", typeof(string));
            table.Columns.Add("Code", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            table.Columns.Add("Type", typeof(bool));
            table.Columns.Add("BillID", typeof(string));
            table.Columns.Add("BillIndex", typeof(string));
            return table;

        }
        /// <summary>
        /// 创建title表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillTitle()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ShopName", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("Tel2", typeof(string));
            table.Columns.Add("ShopId", typeof(int));
            table.Columns.Add("BillOrderCode", typeof(string));
            table.Columns.Add("shopKeeper", typeof(string));
            table.Columns.Add("QQ", typeof(string));
            table.Columns.Add("BillId", typeof(string));
            table.Columns.Add("CreateTime", typeof(DateTime));
            table.Columns.Add("Balance", typeof(double));
            table.Columns.Add("BillCount", typeof(double));
            table.Columns.Add("CreditLevel", typeof(int));
            table.Columns.Add("Salesman", typeof(string));
            table.Columns.Add("Adress", typeof(string));
            table.Columns.Add("CheckOutType", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            table.Columns.Add("Para_TotalNum", typeof(int));
            table.Columns.Add("Para_TotalAreas", typeof(double));
            table.Columns.Add("Para_TotalCount", typeof(double));
            table.Columns.Add("Para_Sum", typeof(double));
            table.Columns.Add("Para_TotalPrintCount", typeof(double));
            table.Columns.Add("Para_TotalProductCount", typeof(double));
           



            return table;

        }
        /// <summary>
        /// 创建明细产品
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("OrderInfoCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("Remark", typeof(string));
            table.Columns.Add("OrderBagName", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建明细收费项目表
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillSubDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("DetailName", typeof(string));
            table.Columns.Add("Spec", typeof(string));
            table.Columns.Add("Num", typeof(int));
            table.Columns.Add("UnitPrice", typeof(double));
            table.Columns.Add("Count", typeof(double));
            table.Columns.Add("Area", typeof(double));
            table.Columns.Add("Type", typeof(int));
            table.Columns.Add("DetailRemark", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建发货信息表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateFahuoTable()
        {
            DataTable table = new DataTable("tableBottom");
            table.Columns.Add("ConsigneeName", typeof(string));
            table.Columns.Add("ConsigneeTelePhone", typeof(string));
            table.Columns.Add("DistributionAddress", typeof(string));
            table.Columns.Add("BusTimetable", typeof(string));//收货单位
            table.Columns.Add("ReMark", typeof(string));
            table.Columns.Add("DistributionMode", typeof(string));
            table.Columns.Add("Fahuodanwei", typeof(string));
            table.Columns.Add("Fahuodianhua", typeof(string));

            return table;
        }
    }
}
